package com.spring4.mvc.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

@Configuration
@MapperScan({ "com.lp.mvc.mysql.mapper"
//	, "com.lp.mvc.oracle.mapper" 
	})
// @EnableTransactionManagement
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
public class DataConfig 
//implements TransactionManagementConfigurer 
{

	//[start]
	
	
	
	//[end]
	@Value("${jdbc.mysql.driverClassName}")
	private String mysql_driverClassName;

	@Value("${jdbc.mysql.url}")
	private String mysql_url;

	@Value("${jdbc.mysql.username}")
	private String mysql_username;

	@Value("${jdbc.mysql.password}")
	private String mysql_password;

	@Value("${jdbc.mysql.databaseName}")
	private String mysql_databaseName;
 

	private final static String JDBC_CONFIG_PATH = "jdbc.xml";

	// @Inject
	// private Environment environment;

	@Bean
	public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
		PropertyPlaceholderConfigurer pp = new PropertyPlaceholderConfigurer();
		pp.setLocations(new Resource[] { new ClassPathResource(JDBC_CONFIG_PATH) });
		return pp;
	}

	@Bean
	public DataSource dataSource() {
		// mysql

		System.out.println("mysql ~~~~~~~~~~~~~~~~~~~~~~~~~");
		MysqlDataSource mysqlDataSource = new MysqlDataSource();
		mysqlDataSource.setURL(mysql_url);
		mysqlDataSource.setUser(mysql_username);
		mysqlDataSource.setPassword(mysql_password);
		// mysqlDataSource.setDatabaseName(jdbcDatabaseName);
		return mysqlDataSource;
	}
 

	@Bean
	public PlatformTransactionManager annotationDrivenTransactionManager() {
		return new DataSourceTransactionManager(dataSource());
	}

	@Bean
	public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
		SqlSessionFactoryBean sf = new SqlSessionFactoryBean();
		sf.setDataSource(dataSource());
		sf.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/lp/mvc/mysql/xml/*.xml"));
		// sf.setConfigLocation(new
		// DefaultResourceLoader().getResource("classpath:mybatis.xml"));

		// sf.setTypeAliasesPackage("com.lp.mvc.mysql.model");

		return sf.getObject();
	}

	@Bean
	MapperScannerConfigurer DBmapperScannerConfigurer() {
		MapperScannerConfigurer sc = new MapperScannerConfigurer();
		sc.setBasePackage("com.lp.mvc.mysql.mapper");
		 sc.setAnnotationClass(Repository.class);
		// sc.setSqlSessionFactory(sqlSessionFactory());
		return sc;
	}

}